﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;
using Bricks.Business.Data.Repositories.Account;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IContractInfoService))]
    internal class ContractInfoService : IContractInfoService
    {
        [Import]
        protected IContractInfoRepository ContractInfoRepository { get; set; }
        [Import]
        protected ICommonContractInfoRepository CommonContractInfoRepository { get; set; }
        [Import]
        protected IOrgInfoRepository OrgInfoRepository { get; set; }

        [Import]
        protected IProjectInfoRepository ProjectInfoRepository { get; set; }


        public IQueryable<ContractInfo> ContractInfos
        {
            get { return ContractInfoRepository.Entities; }
        }

        /// <summary>
        /// 获取完整的合同名称（项目-类型-名称）
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public string GetFullContractNameByID(List<Guid> ids)
        {
            string result = string.Empty;
            if (ids != null && ids.Count > 0)
            {
                List<string> fullContractNames = new List<string>();

                //合同
                var contractlist = from contract in ContractInfoRepository.Entities
                                   join project in ProjectInfoRepository.Entities on contract.PID equals project.ID
                                   where ids.Contains(contract.ID)
                                   select new
                                   {
                                       ProjectName = project.ProjectName,
                                       ContractName = contract.ContractName
                                   };
                if (contractlist != null && contractlist.Count() > 0)
                {
                    foreach (var item in contractlist)
                    {
                        fullContractNames.Add(string.Format("{0}-{1}", item.ProjectName, item.ContractName));
                    }
                }

                //common合同
                var commonContractlist = from commonContract in CommonContractInfoRepository.Entities
                                         join project in ProjectInfoRepository.Entities on commonContract.PID equals project.ID
                                         where ids.Contains(commonContract.ID)
                                         select new
                                         {
                                             ProjectName = project.ProjectName,
                                             ContractName = commonContract.ContractName
                                         };
                if (commonContractlist != null && commonContractlist.Count() > 0)
                {
                    foreach (var item in commonContractlist)
                    {
                        fullContractNames.Add(string.Format("{0}-{1}", item.ProjectName, item.ContractName));
                    }
                }
                result = string.Join(",", fullContractNames);
            }
            return result;
        }

    }
}